/*
This is part of Screenbar, which is based in part on the Litestep
shell source code.

Copyright (C) 2008-2009 Jim Babcock
Copyright (C) 1997-2009 The LiteStep Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include "stdafx.h"

class MinimapWindowPainter
{
public:
	HBRUSH windowBrush;
	HPEN pen;
	HBRUSH titleBarBrush;
	HBRUSH focusedTitleBarBrush;
	HBRUSH oldBrush;
	HPEN oldPen;
	RCSettings *settings;
	HDC backBuffer;
	MinimapElement *minimap;
	
	MinimapWindowPainter(RCSettings *settings, HDC backBuffer, MinimapElement *minimap)
		:settings(settings), backBuffer(backBuffer), minimap(minimap)
	{
		windowBrush = CreateSolidBrush(minimap->minimapWindowColor);
		titleBarBrush = CreateSolidBrush(minimap->minimapTitleBarColor);
		focusedTitleBarBrush = CreateSolidBrush(minimap->minimapFocusedTitleBarColor);
		pen = CreatePen(PS_SOLID, 1, minimap->minimapWindowBorderColor);
		oldBrush = (HBRUSH)SelectObject(backBuffer, windowBrush);
		oldPen = (HPEN)SelectObject(backBuffer, pen);
	}
	
	~MinimapWindowPainter()
	{
		SelectObject(backBuffer, oldBrush);
		SelectObject(backBuffer, oldPen);
		DeleteObject(windowBrush);
		DeleteObject(titleBarBrush);
		DeleteObject(focusedTitleBarBrush);
		DeleteObject(pen);
	}
	
	void paintWindow(WindowData *window, RECT r, HICON icon, POINT iconPos)
	{
		Rectangle(backBuffer, r.left, r.top, r.right, r.bottom);
		
		RECT rtitle;
		if(minimap->minimapTitleBarsThickness && !window->isPopup)
		{
			if(window->isFocused())
				SelectObject(backBuffer, focusedTitleBarBrush);
			else
				SelectObject(backBuffer, titleBarBrush);
			
			rtitle.left = r.left;
			rtitle.top = r.top;
			rtitle.right = r.right;
			rtitle.bottom = r.top + minimap->minimapTitleBarsThickness;
			// save the bottom of the titlebar as the top of the rest of the window
			r.top = rtitle.bottom;
			
			Rectangle(backBuffer,
				rtitle.left, rtitle.top, rtitle.right, rtitle.bottom);
			SelectObject(backBuffer, windowBrush);
		}
		
		int iconSize = minimap->minimapIconSize;
		if(icon && iconSize)
			DrawIconEx(backBuffer, iconPos.x, iconPos.y, icon, iconSize, iconSize, 0, NULL, DI_NORMAL);
	}
};

void MinimapElement::draw(HDC drawContext, const LayoutCacheNode *node)
{
	MinimapWindowPainter painter(settings, drawContext, this);
	RECT rect = getRect(node->boundingRect);
	
	vector<WindowData*> windows;
	windowTracker->getWindows(node->context.desk, windows);
	
	// Iterate in bottom-to-top order (Painter's algorithm)
	for(vector<WindowData*>::reverse_iterator it=windows.rbegin(); it!=windows.rend(); it++)
	{
		WindowData *windowData = *it;
		
		if(windowData->isMinimized())
			continue;
		
		POINT iconPos;
		
		RECT r = screenToMinimapPos(node, windowData->lastScreenPos, &iconPos);
			r.left += rect.left;
			r.right += rect.left;
			r.top += rect.top;
			r.bottom += rect.top;
			iconPos.x += rect.left;
			iconPos.y += rect.top;
		
		if(!windowData->parent && !windowData->isPopup)
			painter.paintWindow(windowData, r, windowData->getIcon(minimapIconSize), iconPos);
		else
			painter.paintWindow(windowData, r, NULL, iconPos);
	}
}

